<?php
	include_once $_SERVER["DOCUMENT_ROOT"].'/service/entite/albumphoto/AlbumPhotoEntite.php';
	include_once $_SERVER["DOCUMENT_ROOT"].'/persistence/albumphoto/AlbumPhotoDAO.php';
	include_once($_SERVER["DOCUMENT_ROOT"].'/outils/properties/PropertiesLoader.php');
	PropertiesLoader::loadProperties($_SERVER["DOCUMENT_ROOT"].'/properties.ini');

	class AlbumPhotoService {
		
		private function AlbumPhotoService() {
			//évite la construction
		}
		
		public static function createAlbumPhoto() {
			return new AlbumPhotoEntite();
		}
		
		public static function getListeMiniature($albumPhotoEntite) {
			$albumPhotoEntite = PhotoDAO::loadListePhotoAlbum($albumPhotoEntite);
			$listeMiniature = Array();
			foreach ($albumPhotoEntite->listePhoto as $photo) {
				$listeMiniature[] = MiniaturePhotoService::getMiniature($photo);
			}
			return $listeMiniature;
		}
		
		public static function getAlbumPhotoForModification($id) {
			return AlbumPhotoDAO::getAlbumPhotoByID($id);
		}
		
		public static function getAlbumPhotoByID($id, $loadListePhotoAlbum = false) {
			$albumPhoto = AlbumPhotoDAO::getAlbumPhotoByID($id);
			if ($albumPhoto != null) {
				AlbumPhotoDAO::loadCouverture($albumPhoto);
				if ($loadListePhotoAlbum) {
					AlbumPhotoService::loadListePhotoAlbum($albumPhoto);
				}
			}
			return $albumPhoto;
		}
		
		public static function supprimerAlbumPhoto($id) {
			//TODO supprimer le répertoire?
			return AlbumPhotoDAO::delete(AlbumPhotoDAO::getAlbumPhotoByID($id));
		}
		
		public static function save(AlbumPhotoEntite $albumPhotoEntite) {
			$isCreation = $albumPhotoEntite->id == Entite::getUnpersistedId();
			$result = AlbumPhotoDAO::save($albumPhotoEntite);
			if ($isCreation) {
				AlbumPhotoService::createRepertoireAlbumPhoto($albumPhotoEntite->id);
			}
			return $result;
		}
		
		/*
		 * Utilisé par la reprise de données des albums
		 */
		public static function insertWithValues($id, $nom, $commentaire) {
			$albumPhotoEntite = new AlbumPhotoEntite();
			$albumPhotoEntite->id = $id;
			$albumPhotoEntite->nom = $nom;
			$albumPhotoEntite->commentaire = $commentaire;
			$albumPhotoEntite->couverture = null;
			AlbumPhotoDAO::insertWithId($albumPhotoEntite);
			return $albumPhotoEntite;
		}
		
		public static function getListeAlbumPhoto() {
			return AlbumPhotoDAO::getListeAlbumPhoto();
		}
		
		public static function loadListePhotoAlbum($albumPhoto, $refresh=true) {
			if ($albumPhoto->listePhoto == null || $refresh) {
				$albumPhoto = PhotoDAO::loadListePhotoAlbum($albumPhoto);
			}
			return $albumPhoto;
		}
		
		public static function createRepertoireAlbumPhoto($albumPhotoId) {
			$cheminAlbumPhoto = $_SERVER["DOCUMENT_ROOT"].PropertiesLoader::getProperty("albumPhoto.chemin");
			mkdir($cheminAlbumPhoto."/".$albumPhotoId);
		}
		
		public static function getNomCompletMiniatureAlbum($albumPhoto) {
			return "";
		}
		
		public static function loadCouverture($albumPhotoEntite) {
			return  AlbumPhotoDAO::loadCouverture($albumPhotoEntite);
		}
	}
?>